package com.tucc.hello.leetcode.no1_400.no1_200.no200;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        char[][] grid = {
                {'1', '1', '0', '0', '0'},
                {'1', '1', '0', '0', '0'},
                {'0', '0', '1', '0', '0'},
                {'0', '0', '0', '1', '1'}
        };

        System.out.println(solution.numIslands(grid));
    }

    public int numIslands(char[][] grid) {
        int num = 0;
        for(int i = 0;i < grid.length;i ++){
            for(int j = 0;j < grid[i].length;j ++){
                if(grid[i][j] == '1'){
                    changeIsland(grid, i, j);
                    num ++;
                }
            }
        }
        return num;
    }

    public void changeIsland(char[][] grid,int i, int j){
        if(i >= 0 && i < grid.length){
            if(j >= 0 && j < grid[i].length){
                if(grid[i][j] == '1') {
                    grid[i][j] = '2';
                    changeIsland(grid, i + 1, j);
                    changeIsland(grid, i - 1, j);
                    changeIsland(grid, i, j + 1);
                    changeIsland(grid, i, j - 1);
                }
            }
        }
    }
}
